﻿using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Service.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LuShanTea.Service
{
    class GoodImgSrcService : IGoodImsSrcService
    {
        // 为商品添加图片和压缩图
        public long AddNew(GoodImgSrcDTO goodImgSrc)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<GoodImgSrc> bs = new BaseService<GoodImgSrc>(ctx);
                // 判断要添加的图片被压缩图在图片表是否存在
                bool exist = bs.GetAll().Any(g => g.ImgSrc == goodImgSrc.ImgSrc && g.ThumImgSrc == goodImgSrc.ThumImgSrc);
                if (exist)
                {
                    throw new ArgumentException("该图片和他的压缩图已存在~~~");
                }
                GoodImgSrc gImgSrc = new GoodImgSrc();
                gImgSrc.DescImgSrc = goodImgSrc.DescImgSrc;
                gImgSrc.GoodId = goodImgSrc.GoodId;
                gImgSrc.ImgSrc = goodImgSrc.ImgSrc;
                gImgSrc.ThumImgSrc = goodImgSrc.ThumImgSrc;

                ctx.GoodImgSrcs.Add(gImgSrc);
                ctx.SaveChanges();
                return gImgSrc.Id;
            }
        }

        public GoodImgSrcDTO[] GetFirstImgByGoodId(IEnumerable<long> goodId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<GoodImgSrc> bs = new BaseService<GoodImgSrc>(ctx);

                List<GoodImgSrc> list = new List<GoodImgSrc>();

                foreach (var item in goodId)
                {
                    var goodImgs = bs.GetAll().FirstOrDefault(e => e.GoodId == item);
                    if (goodImgs == null)
                    {
                        throw new ArgumentException("参数异常，找不到id=" + item + "的商品");
                    }
                    list.Add(goodImgs);
                }
                return list.Select(e => ToDTO(e)).ToArray();
            }
        }

        public GoodImgSrcDTO GetFirstThumImgByGoodId(long goodId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<GoodImgSrc> bs = new BaseService<GoodImgSrc>(ctx);
                var data = bs.GetAll().FirstOrDefault(g => g.GoodId == goodId);

                return data == null ? null : ToDTO(data);
            }
        }

        public GoodImgSrcDTO GetFirstImgByGoodId(long goodId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<GoodImgSrc> bs = new BaseService<GoodImgSrc>(ctx);


                var goodImg = bs.GetAll().FirstOrDefault(e => e.GoodId == goodId);

                return goodImg == null ? null : ToDTO(goodImg);
            }
        }

        // 获取指定商品的所有图片
        public GoodImgSrcDTO[] GetImgsByGoodId(long goodId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<GoodImgSrc> bs = new BaseService<GoodImgSrc>(ctx);
                var datas = bs.GetAll().Where(g => g.GoodId == goodId);
                if (datas == null)
                {
                    //throw new ArgumentException($"图片表中不存在商品id为{goodId}的商品");
                    return null;
                }
                return datas.Include(g => g.Good).AsNoTracking().ToList().Select(g => ToDTO(g)).ToArray();
            }
        }

        public void MakeDelete(long goodImgSrcId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<GoodImgSrc> bs = new BaseService<GoodImgSrc>(ctx);
                bs.MakeDelete(goodImgSrcId);
            }
        }


        public GoodImgSrcDTO GetImgSrcByCategoryId(long categoryId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<GoodImgSrc> bs = new BaseService<GoodImgSrc>(ctx);
                var datas = bs.GetAll().Where(g => g.GoodId == categoryId).SingleOrDefault();
                if (datas == null)
                {
                    throw new ArgumentException($"图片表中不存在商品id为{categoryId}的商品");
                }
                return ToDTO(datas);
            }
        }

        public GoodImgSrcDTO ToDTO(GoodImgSrc gImgSrc)
        {
            GoodImgSrcDTO dto = new GoodImgSrcDTO();
            dto.CreateDateTime = gImgSrc.CreateDateTime;
            dto.DescImgSrc = gImgSrc.DescImgSrc;
            dto.GoodId = gImgSrc.GoodId;
            dto.Id = gImgSrc.Id;
            dto.ImgSrc = gImgSrc.ImgSrc;
            dto.ThumImgSrc = gImgSrc.ThumImgSrc;

            return dto;
        }
    }
}
